home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Philippe Verdy <100105.3120@compuserve.com>
- Newsgroups: comp.lang.c++,ualberta.cmput.201
- Subject: Re: class/function pointer help!
- Date: 31 Mar 1996 18:56:26 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4jmkgq$hei@dub-news-svc-3.compuserve.com>
- NNTP-Posting-Host: hd80-162.compuserve.com
-
- ryangall@gpu.srv.ualberta.ca (Ryan Gallagher) s'Θcrit :
- > Hi, I have a class that uses a function pointer. The problem Im having is
- > that the pointer doesnt want to access the function when the function is a
- > local function to the calling class. Here is what I mean....
- >
- > class expression
- > {
- > public:
- >
- > string S;
- > list L;
- > .
- > .
- > int initlist();
- > .
- > int isexpr(int);
- > };
- >
- >
- > list, and string are both classes.
- >
- > char * string::POP( int (* separator)(int c) )
- > {
- >
- > int n=0,m=0,r,size=strlen(S);
- > char *A,*B,*C;
- >
- > A=new char[size+1]; // allocate space for strings A & B
- > .
- > .
- >
- >
- > // find the first occurence of a separator character
- > while(!(*separator)(A[n]) && A[n]) n++;
- >
- > .
- > .
- > .
- > return A;
- > }
- >
- > // heres the calling function
- >
- > int expression::initlist(void)
- > {
- > char *temp;
- > float F;
- > char C;
- >
- > L.init();
- >
- > while((temp=S.POP( ::isexpr )))
- > {
- > F=C=0;
- > assert(convert(F,C,temp));
- > L.Add(L.Assign(F,C));
- > delete[] temp;
- > }
- >
- > return 0;
- > }
- >
- >
- > this works when I define isexpr(int) globally, but I want to beable to
- > call function expression::isexpr(int).....when I do this...
- >
- > while((temp=S.POP( isexpr )))
- > {
- >
- > the compiler say:" member function must be called or its address taken."
- >
- > how do I declare the member function as a function pointer?!
- >
- >
- >
- >
- > thanks
- > ------
- > Please mail me back if possible.
- >
- This is not a problem of your compiler, but a C++ language
- feature.
- >
- A member function is not a standard function.
- So your isexpr() member function is not compatible with your
- S.POP() formal argument.
- This is because member functions have an hidden supplementary
- argument which references the instance on which it applies.
- The only way to manage with it would be to declare your
- member function isexpr() as a static member function, so that
- the instance reference is not passed. (So it will not be
- available in the body of that function).
-
- Declare S.POP() like this :
- class string
- { ...
- static char * POP( int (* separator)(int c) );
- }
-
-
- The other way is to declare the type of the instance reference
- (implicitly passed to your member function) within the
- argument of S.POP(). Like this, the pointed function will have
- access to its backgound instance.
-
- Declare S.POP() like this :
- class string
- { ...
- char * POP( int (expression::* separator)(int c) );
- }
-
- This explictly declares that the function pointed by
- the "separator" argument, is a member of the "expression"
- class, and so it has an implicit argument which is an
- expression reference...
-
- In summary, function pointers are not compatible to
- non static member function pointers.
- In addition, a method pointer from a given class is not compatible
- with a method pointer from another class which is not
- derived from the first one.
-
- However, you can pass a pf method pointer like:
- int (X::*pf)(U, V);
- as if it was in fact declared as :
- int (Y::*pf)(U, V);
- provided X is derived from Y.
-
- Its like if you had declared the function pointer:
- static int (*pf)(X& x, U u, V v);
- and tried to pass it as an argument to a function which accepts
- static int (*pf)(Y& y, U u, V v);
- and then use the x argument explicitly to access the members
- of the actual y parameter.
-
- The last declaration used is what you would use, if you have
- to manage your pointers to several class types.
-
- I hope this will help you...
-
-